/** 
* 
* @desc 首页登录组件 私有组件
*/
<template>
    <div class="index-login">
        <p>查看竞拍品更详细内容请注册/登录</p>
        <input  type="number" class="index-login__user" v-model="userAccount"  @focus="focusUserNameFn" id="UserId" placeholder="手机号码"/>
        <div class="index-login__code">
            <input type="number"  v-model="verificationCode" placeholder="验证码" @focus="focusFn" id="inputId"/>
            <span @click="getVerCodeFn" v-if="!showCountTime">获取验证码</span>
            <span v-else>{{time}}S</span>
        </div>
        <button class="index-login__btn" @click="login">注册/登录</button>
        <transition name="tips" >
            <div class="tip" v-if="message">{{message}}</div>
        </transition>
    </div>
</template>
<script>
export default {
    data() {
        return {
            time: 60,                           // 默认倒计时时间60秒
            tipTime:2,
            message:'',
            tipTimer:null,
            codeTimer: null,                    // 接受时间
            userAccount: '',                    // 用户登录账号
            showCountTime: false,               // 是否显示倒计时
            verificationCode: '',               // 验证码
        }
    },
    methods: {
        focusFn(){
            document.querySelector('#inputId').scrollIntoView();
        },
        focusUserNameFn() {
            document.querySelector('#UserId').scrollIntoView();
        },
        /**
         * @author yang
         * @desc 获取验证码 这里调用ajax 获取验证码
         */
        getVerCodeFn() {
            console.log(this.userAccount.length)
            if(!this.userAccount){
                this._handleTipFn('请填写登录账号')
                return
            }
            if(this.userAccount.length < 11) {
                this._handleTipFn('请输入11位电话号码')
                return
            }
            
            this._getCode()
            this.codeTimer = setInterval(() => {
                this.time--
                if(this.time <=0){
                    this.showCountTime = false
                    clearInterval(this.codeTimer)     
                } 
            },1000)
            this.showCountTime = true
        },
        _getCode() {
            this.$ajax({
                type: 'GET',
                api: 'member/authcode',
                params: {
                    mobile: this.userAccount
                }
            }).then(res => {
                this.verificationCode = res.code === 200 ?  res.data.checkcode : ''
            }).catch((err) => {
                this._handleTipFn(res.data.message)
            })
        },
        _handleTipFn(message) {
            this.message = message
            this.tipTimer = setInterval(() => {
                this.tipTime--
                // 这里可以定义个方法通过接口获取验证码 this.verificationCode = xxxx
                
                if(this.tipTime <=0){
                    this.tipTime = 2
                    this.message = ''
                    clearInterval(this.tipTimer)     
                } 
            },1000) 
        },
        /**
         * @desc 这里是点击登录按钮 获取验证码之后调用你的登录接口
         */
        login() {
            if(!this.userAccount){
                this._handleTipFn('请填写登录账号')
                return
            }
            if(this.userAccount.length < 11) {
                this._handleTipFn('请输入11位电话号码')
                return
            } 
            if(!this.verificationCode){
                this._handleTipFn('请输入验证码')
                return
            }
            this.$ajax({
                api: 'mobilelogin',
                contentType: 'json',
                params: {
                    authcode: this.verificationCode,
                    mobile: this.userAccount
                }
            }).then(res => {
                console.log(res)
                if(res.code === 200) {
                    this._handleTipFn('登录成功')
                    this.$emit('getUserAccountFn', this.userAccount)
                }else {
                   this._handleTipFn(res.data.message) 
                }
            }).catch(err => {
                this._handleTipFn(err.message)
            })
            
        }
    }
}
</script>
<style lang="scss" scoped rel="stylesheet/scss">
    .index-login{
        flex: 1;
        display: flex;
        align-items: center;
        flex-direction: column;
        p{
            font-size: 15px;
            color: #484848;
            font-weight: 600;
            margin-bottom: 20px;
        }
        .tip{
            position: fixed;
            left: 50%;
            margin-left: -25%;
            bottom: 18%;
            width: 50%;
            background-color: rgba(0, 0, 0, 0.7);
            line-height: 1.8rem;
            padding: 0.5rem 0.6rem;
            color: #fff;
            border-radius: 0.5rem;
            text-align: center;
            font-size: 16px;
            word-break: break-all;
            z-index: 30;
        }
        .index-login__user{
            width: 290px;
            height: 40px;
            display: inline-block;
            line-height: 40px;
            background: #F6F6F6;
            border-radius: 5px;
            color: #484848;
            font-size: 15px;
            outline: none;
            box-sizing: border-box;
            padding-left: 18px;
            margin-bottom: 17px;
        }
        .index-login__code{
            width: 290px;
            height: 40px;
            line-height: 40px;
            border-radius: 5px;
            display: flex;
            padding-right: 10px;
            box-sizing: border-box;
            background: #F6F6F6;
            >input{
                flex: 1;
                background: #F6F6F6;
                display: block;
                outline: none;
                box-sizing: border-box;
                text-indent: 18px;
                width: 80%;
                border-top-left-radius: 5px;
                border-bottom-left-radius: 5px;
            }
            >span{
                color: #E6512D
            }
        }
        .index-login__btn{
            width: 290px;
            height: 40px;
            line-height: 40px;
            background: #E6512D;
            color: #fff;
            font-size: 17px;
            outline: none;
            border: none;
            margin-top: 17px;
            border-radius: 5px;
        }
        .tips-transition {
            -webkit-transition: all .4s;
        }
        .tips-enter {
            -webkit-transform: scale3d(.8, .8, 1);
            opacity: 0;
        }
        .tips-leave {
            -webkit-transform: scale3d(1.2, 1.2, 1);
            opacity: 0;
        }
    }
</style>